﻿using System.Collections.Generic;
using System.Linq;
using Authorization;
using Bussiness;
using Bussiness.Mangement;
using Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Commons;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using System.IO;
using Entity.Project;
using Bussiness.Mangement.Project;

namespace NetCoreAPI.Controllers.Portal
{
    /// <summary>
    /// 用户
    /// </summary>
    [CheckLogin]
    [ApiController]
    public class UserController : PortalApiController
    {

        /// <summary>
        /// 修改用户头像昵称和个性签名
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPut]
        public  JsonResult EditUserInfo([FromBody] User user)
        {
            try
            {
                if (user==null || Current.UId==0)
                {
                    throw new System.Exception("user为空。");
                }
                string errorMessage;
                var xtU= ServiceHelp.GetUserService.GetById(Current.UId);
                if (xtU == null )
                {
                    throw new System.Exception("未查询到用户信息。");
                }
                if (!string.IsNullOrWhiteSpace(user.NickName))
                {
                    xtU.NickName = user.NickName;
                }
                if (!string.IsNullOrWhiteSpace(user.HeadImgUrl))
                {
                    xtU.HeadImgUrl = user.HeadImgUrl;
                }
                if (!string.IsNullOrWhiteSpace(user.Signature))
                {
                    xtU.Signature = user.Signature;
                }

                var result = UserBussiness.Init.EditUserAndNotEditPwd(xtU, out errorMessage);
                if (result == null)
                {
                    return new JsonResult(HttpResult.Success(HttpResultCode.AddFail, errorMessage, result));
                }                                                                                                                                                                                 
                else
                {
                    return new JsonResult(HttpResult.Success(result));
                }
            }
            catch (System.Exception ex)
            {
                return new JsonResult(HttpResult.Success(HttpResultCode.AddFail, ex.Message, null));
            }
        }

        /// <summary>
        /// 获取当前访问用户的信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public  JsonResult GetUserInfo()
        {
            try
            {
                int id = Current.UId;
                if (id <= 0)
                {
                    throw new System.Exception("id不能小于1");
                }
                User user = ServiceHelp.GetUserService.GetById(id);
                ////牌局 最大赢取 入池率 赢率
                //List<GameRecord> gameRecords = ServiceHelp.GetGameRecordService.GetAllList(x => x.UserId == user.Id).ToList();
                //double rcl = 0, yl = 0;
                //long maxJetton = 0;
                //Wands maxWands = null;
                //if (gameRecords != null && gameRecords.Count > 0)
                //{
                //    double rcs = gameRecords.FindAll(x => x.Jetton != 0).Count, ys = gameRecords.FindAll(x => x.IsWin).Count;
                //    rcl = rcs == 0 ? 0 : rcs / gameRecords.Count;
                //    yl = ys == 0 ? 0 : ys / gameRecords.Count;
                //    maxJetton = gameRecords.Max(x => x.Jetton);
                //    maxWands = gameRecords.OrderByDescending(x => x.Wands.LevelNum).FirstOrDefault().Wands;
                //}
                var result = new
                {
                    user.Area,
                    user.Email,
                    user.HeadImgUrl,
                    user.Id,
                    user.Mobile,
                    user.Name,
                    user.NickName,
                    user.Signature,
                    //user.Gold,
                    //rcl,
                    //yl,
                    //maxJetton = maxJetton,
                    //maxWands,
                    //GameRecords = gameRecords != null ? gameRecords.OrderByDescending(x => x.CreatedAt).Take(50).ToList() : gameRecords,
                    Sex = user.Sex.GetDisplayName(),
                    CreatedAt = user.CreatedAt.ToLongString()
                };
                return new JsonResult(HttpResult.Success(result));
            }
            catch (System.Exception ex)
            {
                return new JsonResult(HttpResult.Success(HttpResultCode.SelectFail, "未获取到用户信息。", ex.Message));
            }
        }

        /// <summary>
        /// 根据id获取用户信息
        /// </summary>
        /// <returns></returns>
        [HttpGet("id")]
        public JsonResult GetUserById(int id)
        {
            try
            {
                if (id <= 0)
                {
                    throw new System.Exception("id不能小于1");
                }
                User user = ServiceHelp.GetUserService.GetById(id);
                ////牌局 最大赢取 入池率 赢率
                //List<GameRecord> gameRecords = ServiceHelp.GetGameRecordService.GetAllList(x => x.UserId == user.Id).ToList();
                //double rcl=0, yl = 0;
                //long maxJetton = 0;
                //Wands maxWands = null;
                //if (gameRecords!=null && gameRecords.Count>0)
                //{
                //    double rcs = gameRecords.FindAll(x => x.Jetton != 0).Count, ys = gameRecords.FindAll(x => x.IsWin).Count;
                //    rcl = rcs==0?0: rcs / gameRecords.Count;
                //    yl = ys == 0 ? 0 : ys / gameRecords.Count;
                //    maxJetton = gameRecords.Max(x => x.Jetton);
                //    maxWands = gameRecords.OrderByDescending(x => x.Wands.LevelNum).FirstOrDefault().Wands;
                //}
                var result = new
                {
                    user.Area,
                    user.Email,
                    user.HeadImgUrl,
                    user.Id,
                    user.Mobile,
                    user.Name,
                    user.NickName,
                    user.Signature,
                    user.Gold,
                    //rcl,
                    //yl,
                    //maxJetton = maxJetton,
                    //maxWands,
                    //GameRecords = gameRecords != null ? gameRecords.OrderByDescending(x => x.CreatedAt).Take(50).ToList(): gameRecords,
                    Sex = user.Sex.GetDisplayName(),
                    CreatedAt = user.CreatedAt.ToLongString()
                };
                return new JsonResult(HttpResult.Success(result));
            }
            catch (System.Exception ex)
            {
                return new JsonResult(HttpResult.Success(HttpResultCode.SelectFail, "未获取到用户信息。", ex.Message));
            }

        }
    }
}
